package com.wc.AlgoOJ_homework.ALOJ模块测试Ⅱ.B_路径;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Author congge
 * @Date 2024/5/16 14:17
 * @description http://43.138.190.70:8888/p/638?tid=663e2cd8d39d10926ed3fa74
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 10010, M = 2000010, idx = 1;
    static long INF = 0x3f3f3f3fL * 0x3f3f3f3f;
    static long[] dist = new long[N];
    static boolean[] st = new boolean[N];
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static int n, m;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            m = sc.nextInt();
            for (int i = 1; i <= n; i++) h[i] = 0;
            idx = 1;
            for (int i = 1; i <= n; i++) {
                for (int j = i + 1; j <= n; j++) {
                    if (j - i <= m) {
                        int c = i * j / gcd(i, j);
                        add(i, j, c);
                        add(j, i, c);
                    } else break;
                }
            }
//            spfa();
            dijkstra();
            out.println(dist[n]);
        }
        out.flush();
    }

    static void spfa() {
        Arrays.fill(dist, 1, n + 1, INF);
        Arrays.fill(st, 1, n + 1, false);
        dist[1] = 0;
        Queue<Integer> q = new LinkedList<>();
        q.add(1);
        st[1] = true;
        while (!q.isEmpty()) {
            int t = q.poll();
            st[t] = false;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[j] > dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    if (!st[j]) {
                        q.add(j);
                        st[j] = true;
                    }
                }
            }
        }
    }

    static void dijkstra() {
        Arrays.fill(dist, 1, n + 1, INF);
        Arrays.fill(st, 1, n + 1, false);
        dist[1] = 0;
        PriorityQueue<long[]> q = new PriorityQueue<>(Comparator.comparingLong(o -> o[0]));
        q.add(new long[]{dist[1], 1});

        while (!q.isEmpty()) {
            int t = (int) q.poll()[1];
            if (st[t]) continue;
            st[t] = true;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[j] > dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    q.add(new long[]{dist[j], j});
                }
            }
        }
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
